package org.vordel.incubator.examples.management_api;

import java.io.File;
import java.util.Collection;

import com.vordel.es.ESPK;
import com.vordel.es.Entity;
import com.vordel.es.EntityType;
import com.vordel.es.Field;
import com.vordel.es.FieldType;
import com.vordel.es.Value;
import com.vordel.es.fed.FederatedEntityStore;
import com.vordel.pd.agent.ManagementAgentUtil;
import com.vordel.pd.generated.agent.management.ManagementAgent;



/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws Exception {
    	
        ManagementAgent ma = ManagementAgentUtil.getManagementAgent("http://localhost:8090/runtime/management/ManagementAgent", "admin", "changeme");
        System.out.println("Connected to host: "+ma.getHostName()+" product: "+ma.getProductName()+" version: "+ma.getProductVersion());
        
        File workingDir = new File("c:\\users\\jbregman\\working");
        FederatedEntityStore es = ManagementAgentUtil.getActiveConfigurationFedStore(ma, workingDir);
       
        EntityType webServiceGroupType = es.getTypeForName("WebServiceGroup");
        Entity newWebServiceGroup = new Entity(webServiceGroupType);
        System.out.println(webServiceGroupType.getAllFieldNames());
        
        ESPK rootPK = es.getRootPK();
        EntityType webServiceRegistryType = es.getTypeForName("WebServiceRepository");
        System.out.println(webServiceRegistryType.getAllFieldNames());
        
        FieldType nameFieldType = webServiceRegistryType.getFieldType("name");
        Field nameField = new Field(nameFieldType,"name");
        nameField.addValue(new Value("Web Service Repository"));
        
        Collection<ESPK> children = es.findChildren(rootPK, new Field[] {nameField}, webServiceRegistryType);
        
        ESPK webServiceRepositoryESPK = children.iterator().next();
        
        newWebServiceGroup.setStringField("name","New Web Service Group");
        
        es.addEntity(webServiceRepositoryESPK,newWebServiceGroup);
        
        com.vordel.pd.generated.DeploymentResult result = ManagementAgentUtil.deploy(ma, workingDir);
        
        System.out.println(result.isStatus());
        System.out.println(result.getFailureReason());
        
        
        
    }
        
        
    
}
